// pages/my/order/orderdetail/couponlist.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponlist:[],
    buyid:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.loadCouponlist();
    this.data.buyid = options.buyid;
    console.log('options.buyid=' + options.buyid);
    this.loadToServer(options.buyid);
  },
  loadToServer: function (buyid) {
    var url = app.globalData.webUrl + 'Goods/getCoupon';

    var tdata = { buyid: buyid };

    app.loadDataFromServer(url, this.loadCallBack, tdata);

    app.UpdateList_fun('couponlist', false);
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: res.msg
      });
      return;
    }

    var couponlist = res.result;
    for (var i = 0; i < couponlist.length; i++) {
      couponlist[i].iconurl = app.globalData.ImgwebUrl + couponlist[i].iconurl;
      couponlist[i].showdatefrom = app.formatDateTime(couponlist[i].datefrom,false);
      couponlist[i].showdateto = app.formatDateTime(couponlist[i].dateto,false);
    }

    this.data.isLast = true;
    if (couponlist.length == app.globalData.pageSize) {
      this.data.curpage += 1;
      this.data.isLast = false;
    }

    this.setData({
      couponlist: couponlist
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.getUpdateList('couponlist') == true) {
      this.loadToServer(this.data.buyid);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  consumeCopon:function(e){
    var cuid = e.currentTarget.dataset.consumeid;

    var cinfo = {};
    for (var i = 0; i < this.data.couponlist.length; i++) {
      if (this.data.couponlist[i].consumeid == cuid) {
        cinfo = this.data.couponlist[i];
        break;
      }
    }

    var datastr = JSON.stringify(cinfo);
    datastr = encodeURIComponent(datastr);

    wx.navigateTo({
      url: './coupon?info='+datastr,
    });
  }
})